package com.fan.teamer.process;

import org.apache.log4j.Logger;

import com.fan.teamer.global.TeamerGlobalVariables;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Team;
import com.fan.teamer.service.MemberService;
import com.fan.teamer.service.TeamMemberService;
import com.fan.teamer.service.TeamService;

public class TeamDeleteProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger("TeamDeleteProcess");
	private Team team;
	private Member operator;
	private TeamService teamService;
	private TeamMemberService teamMemberService;
	private MemberService memberService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		TeamDeleteProcess.logger = logger;
	}

	public Team getTeam() {
		return team;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	public Member getOperator() {
		return operator;
	}

	public void setOperator(Member operator) {
		this.operator = operator;
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public TeamMemberService getTeamMemberService() {
		return teamMemberService;
	}

	public void setTeamMemberService(TeamMemberService teamMemberService) {
		this.teamMemberService = teamMemberService;
	}

	public int checkAutority() {
		// check if member has the right to delete team
		String role = memberService.getMemberRoleInTeam(operator, team);
		if (!role.equals(TeamerGlobalVariables.ORGANIZER)) {
			setErrorMessage("Member: " + operator.getEmail()
					+ " cannot delete team " + team.getName());
			return TeamerGlobalVariables.FAILED;
		}
		// make sure team only contains myself
		if (teamService.getTeamMembers(team).size() > 1) {
			setErrorMessage("Team " + team.getName() + " still has members!");
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;

	}

	public int initCheck() {
		if (teamService == null || teamMemberService == null
				|| memberService == null) {
			setErrorMessage("Service is null!");
			return TeamerGlobalVariables.FAILED;
		}
		if (team == null || operator == null) {
			setErrorMessage("domain cannot be null!");
			return TeamerGlobalVariables.FAILED;
		}

		if (!teamService.hasTeam(team.getId())) {
			setErrorMessage("Team " + team.getName() + " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}
		if (!memberService.hasMember(operator.getEmail())) {
			setErrorMessage("Member " + operator.getEmail() + " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}

		if (!teamService.hasMember(team, operator)) {
			setErrorMessage("Team " + team.getName() + " doesn't have Member "
					+ operator.getEmail());
			return TeamerGlobalVariables.FAILED;
		}
		return TeamerGlobalVariables.SUCCESS;
	}

	@Override
	public Integer processImpl() {

		if (initCheck() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}

		if (checkAutority() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}

		// now we can delete team
		try {
			teamService.deleteTeam(team);
			teamMemberService.deleteByTeam(team);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage("Team: " + team.getId() + " cannot be deleted!");
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;
	}

}
